package cn.cjlmonster.demo02.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RadialGradient;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import cn.cjlmonster.demo02.R;
import cn.cjlmonster.demo02.utils.DpUtils;

/**
 * cn.cjlmonster.demo02.widget
 *
 * @author cjl
 * @project Demo02
 * @date 2021/12/22 9:04 下午
 */
public class RingView extends View {

    public RingView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private Paint paint;

    private void init(Context context, @Nullable AttributeSet attrs) {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.ROUND);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        RectF rectf = new RectF(0, 0, width, height);

        int ringWidth = 90;

        RectF rectF = new RectF();
        rectF.left = rectf.left + ringWidth + 5;
        rectF.right = rectf.right - ringWidth - 5;
        rectF.top = rectf.top + ringWidth + 5;
        rectF.bottom = rectf.bottom - ringWidth - 5;

        paint.setStrokeWidth(ringWidth);
        LinearGradient linearGradient = new LinearGradient(0 ,0, 0, ringWidth, Color.RED, Color.BLUE, Shader.TileMode.REPEAT);
        paint.setShader(linearGradient);

        canvas.drawArc(rectF, 135, 270, false, paint);
    }

    private int progress;

    public void setColor(int progress) {
        this.progress = progress;
        invalidate();
    }

}
